package com.agu.mgt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.agu.common.annotation.SysLog;
import com.agu.common.excel.Column;
import com.agu.common.excel.ExcelExport;
import com.agu.entity.sys.SysConfigEntity;
import com.agu.mgt.sys.service.SysConfigService;
import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.CompareUtils;
import com.agu.utils.Query;
import com.agu.utils.xss.XssHttpServletRequestWrapper;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 
 * @author
 * @email
 * @date 2018-10-17 15:58:42
 */
@SuppressWarnings("rawtypes")
@RestController
@Api("系统参数")
@RequestMapping("sysconfig")
public class SysConfigController extends AbstractController {
	@Autowired
	private SysConfigService sysConfigService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ApiOperation(value = "列表", notes = "说明")
	@RequiresPermissions("sysconfig:list")
	public Result list(@RequestBody Map params) {

		// 查询列表数据
		Query query = new Query(params);
		Page<SysConfigEntity> page = sysConfigService.queryPageList(query);
		return ResultGenerator.successPage(page);
	}

	/**
	 * 信息
	 */
	@GetMapping("/info/{id}")
	@ApiOperation(value = "信息", notes = "说明")
	@RequiresPermissions("sysconfig:info")
	public Result info(@PathVariable("id") Long id) {
		SysConfigEntity sysConfig = sysConfigService.queryObject(id);

		return ResultGenerator.successData(sysConfig);
	}

	/**
	 * 保存
	 */
	@SysLog("新增参数")
	@PostMapping("/save")
	@ApiOperation(value = "保存", notes = "说明")
	@RequiresPermissions("sysconfig:save")
	public Result save(@RequestBody SysConfigEntity sysConfig) {
		
		sysConfig.setStatus(1);
		sysConfigService.save(sysConfig);

		// 处理日志
		Map<String, String> cmap = new HashMap<String, String>();
		cmap.put("id", "ID");
		cmap.put("key", "参数编码");
		cmap.put("value", "参数值");
		cmap.put("status", "状态");
		cmap.put("remark", "备注");
		String remark = CompareUtils.getObjectRemark(sysConfig, cmap);
		return ResultGenerator.success("保存成功").setRemark(remark);
	}

	/**
	 * 修改
	 */
	@SysLog("修改系统参数")
	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "说明")
	@RequiresPermissions("sysconfig:update")
	public Result update(@RequestBody SysConfigEntity sysConfig) {
		SysConfigEntity sysConfig1 = sysConfigService.queryObject(sysConfig.getId());
		sysConfig.setStatus(1);
		sysConfigService.update(sysConfig);

		// 处理日志
		Map<String, String> cmap = new HashMap<String, String>();

		cmap.put("id", "ID");
		cmap.put("key", "参数编码");
		cmap.put("value", "参数值");
		cmap.put("status", "状态");
		cmap.put("remark", "备注");

		String remark = CompareUtils.getTwoObjectRemark(sysConfig1, sysConfig, cmap);

		return ResultGenerator.success("修改成功").setRemark(remark);
	}

	/**
	 * 删除
	 */
	@SysLog("删除系统参数")
	@GetMapping("/delete/{id}")
	@ApiOperation(value = "删除", notes = "说明")
	@RequiresPermissions("sysconfig:delete")
	public Result delete(@PathVariable("id") Long id) {
		if (id == null) {
			return ResultGenerator.fail("缺少参数");
		}

		SysConfigEntity sysConfig = sysConfigService.queryObject(id);

		sysConfigService.delete(id);

		// 处理日志
		Map<String, String> cmap = new HashMap<String, String>();
		cmap.put("id", "ID");
		cmap.put("key", "参数编码");
		cmap.put("value", "参数值");
		cmap.put("status", "状态");
		cmap.put("remark", "备注");
		String remark = CompareUtils.getObjectRemark(sysConfig, cmap);
		return ResultGenerator.success("删除成功").setRemark(remark);
	}

	@RequestMapping("/download")
	@ApiOperation(value = "下载", notes = "说明")
	@RequiresPermissions("sysconfig:list")
	public void download(HttpServletRequest request, HttpServletResponse response) {

		HttpServletRequest orgRequest = XssHttpServletRequestWrapper.getOrgRequest(request);

		String query = orgRequest.getParameter("query");

		HashMap querymap = JSON.parseObject(query, HashMap.class);

		List<SysConfigEntity> list = sysConfigService.queryList(querymap);

		if (list != null && list.size() > 0) {
			Map map = new HashMap();

			Map<Integer, String> statusMap = new HashMap<Integer, String>();

			statusMap.put(0, "隐藏");
			statusMap.put(1, "显示");
			map.put("statusMap", statusMap);

			List<Column> clist = new ArrayList();
			Column cloumn = null;

			cloumn = new Column("ID", "id", 1);
			clist.add(cloumn);
			cloumn = new Column("参数名", "key", 1);
			clist.add(cloumn);
			cloumn = new Column("参数值", "value", 1);
			clist.add(cloumn);
			cloumn = new Column("状态 ", "status", 2);
			clist.add(cloumn);
			cloumn = new Column("备注", "remark", 1);
			clist.add(cloumn);
			try {
				ExcelExport export = exportList(list, clist, map);
				if (export != null) {
					this.downExcel(export, response);
					// this.downzip(export, response);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
